<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
  <th:block th:include="include :: header('修改商品')"/>
  <th:block th:include="include :: select2-css"/>
  <th:block th:include="include :: summernote-css"/>
</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
  <form class="form-horizontal m" id="form-product-edit" th:object="${product}">
    <input name="productId" th:field="*{productId}" type="hidden">
    <div class="form-group">
      <label class="col-sm-3 control-label is-required">商品编码：</label>
      <div class="col-sm-8">
        <input name="productCode" th:field="*{productCode}" class="form-control" type="text" required>
      </div>
    </div>
    <div class="form-group">
      <label class="col-sm-3 control-label">商品名称：</label>
      <div class="col-sm-8">
        <input name="productName" th:field="*{productName}" class="form-control" type="text">
      </div>
    </div>
    <div class="form-group">
      <label class="col-sm-3 control-label">显示顺序：</label>
      <div class="col-sm-8">
        <input name="orderNum" th:field="*{orderNum}" class="form-control" type="text">
      </div>
    </div>
    <div class="form-group">
      <label class="col-sm-3 control-label">零售价：</label>
      <div class="col-sm-8">
        <input name="retailPrice" th:field="*{retailPrice}" class="form-control" type="text">
      </div>
    </div>s
    <div class="form-group">
      <label class="col-sm-3 control-label">实售价：</label>
      <div class="col-sm-8">
        <input name="currentPrice" th:field="*{currentPrice}" class="form-control" type="text">
      </div>
    </div>
    <div class="form-group">
      <input name="treeId" type="hidden" id="treeId" th:field="*{categoryId}"/>
      <label class="col-sm-3 control-label is-required">分类：</label>
      <div class="col-sm-8">
        <div class="input-group">
          <input class="form-control" type="text" name="categoryName" onclick="selectCategoryTree()" id="treeName"
                 th:field="*{categoryName}"
                 required>
          <span class="input-group-addon"><i class="fa fa-search"></i></span>
        </div>
      </div>
    </div>
    <div class="form-group">
      <label class="col-sm-3 control-label is-required">品牌：</label>
      <div class="col-sm-8">
        <!--                    <input name="brandId" th:field="*{brandId}" class="form-control" type="text" required>-->
        <select id="brand" class="form-control select2-multiple" th:field="*{brandId}" required>
          <option value="">--请选择--</option>
          <option th:each="brand:${brands}" th:value="${brand.brandId}" th:text="${brand.brandName}"
                  th:disabled="${brand.status == '1'}"></option>
        </select>
      </div>
    </div>
<!--    <div class="form-group">-->
<!--      <label class="col-sm-3 control-label">商品介绍：</label>-->
<!--      <div class="col-sm-8">-->
<!--        <input name="introduce" th:field="*{introduce}" class="form-control" type="text">-->
<!--      </div>-->
<!--    </div>-->
<!--    <div class="form-group">-->
<!--      <label class="col-sm-3 control-label">规格参数：</label>-->
<!--      <div class="col-sm-8">-->
<!--        <input name="specification" th:field="*{specification}" class="form-control" type="text">-->
<!--      </div>-->
<!--    </div>-->
    <div class="form-group">
      <label class="col-sm-3 control-label">商品介绍：</label>
      <div class="col-sm-8">
        <div class="introduce">
        </div>
      </div>
    </div>
    <div class="form-group">
      <label class="col-sm-3 control-label">规格参数：</label>
      <div class="col-sm-8">
        <div class="specification">
        </div>
      </div>
    </div>
    <div class="form-group">
      <label class="col-sm-3 control-label">是否上架：</label>
      <div class="col-sm-8">
        <div class="radio-box" th:each="dict : ${@dict.getType('pro_product_is_putaway')}">
          <input type="radio" th:id="${'status_' + dict.dictCode}" name="isPutaway" th:value="${dict.dictValue}" th:field="*{isPutaway}">
          <label th:for="${'status_' + dict.dictCode}" th:text="${dict.dictLabel}"></label>
        </div>
      </div>
    </div>
    <div class="form-group">
      <label class="col-sm-3 control-label">是否新品：</label>
      <div class="col-sm-8">
        <div class="radio-box" th:each="dict : ${@dict.getType('pro_product_is_new')}">
          <input type="radio" th:id="${'status_' + dict.dictCode}" name="isNew" th:value="${dict.dictValue}" th:field="*{isNew}">
          <label th:for="${'status_' + dict.dictCode}" th:text="${dict.dictLabel}"></label>
        </div>
      </div>
    </div>
    <div class="form-group">
      <label class="col-sm-3 control-label">是否推荐：</label>
      <div class="col-sm-8">
        <div class="radio-box" th:each="dict : ${@dict.getType('pro_product_is_recommend')}">
          <input type="radio" th:id="${'status_' + dict.dictCode}" name="isRecommend" th:value="${dict.dictValue}" th:field="*{isRecommend}">
          <label th:for="${'status_' + dict.dictCode}" th:text="${dict.dictLabel}"></label>
        </div>
      </div>
    </div>
    <div class="form-group">
      <label class="col-sm-3 control-label">状态：</label>
      <div class="col-sm-8">
        <div class="radio-box" th:each="dict : ${@dict.getType('pro_color_status')}">
          <input type="radio" th:id="${'status_' + dict.dictCode}" name="status" th:value="${dict.dictValue}" th:field="*{status}">
          <label th:for="${'status_' + dict.dictCode}" th:text="${dict.dictLabel}"></label>
        </div>
      </div>
    </div>
    <div class="form-group">
      <label class="col-sm-3 control-label">备注：</label>
      <div class="col-sm-8">
        <textarea id="remark" name="remark" class="form-control"></textarea>
      </div>
    </div>
    <div class="hr-line-dashed"></div>
    <div class="form-group">
      <label class="col-sm-3 control-label">商品类型：</label>
      <div class="col-sm-8">
        <input name="typeId" class="form-control" type="hidden" th:field="*{typeId}">
        <input name="typeName" class="form-control" type="text" th:value="*{typeName}" disabled>
      </div>
    </div>

    <div class="form-group" th:each="productCustomRecordValue:${product.productCustomRecordValues}">
      <label class="col-sm-3 control-label is-required"><span th:text="${productCustomRecordValue.customRecordName}"></span>：</label>
      <div class="col-sm-8">
        <!--                    <input name="brandId" th:field="*{brandId}" class="form-control" type="text" required>-->
<!--        <select id="brand" class="form-control select2-multiple" th:field="*{brandId}" required>-->
<!--          <option value="">&#45;&#45;请选择&#45;&#45;</option>-->
<!--          <option th:each="brand:${brands}" th:value="${brand.brandId}" th:text="${brand.brandName}"-->
<!--                  th:disabled="${brand.status == '1'}"></option>-->
<!--        </select>-->
      </div>
    </div>


  </form>
</div>
<th:block th:include="include :: footer"/>
<th:block th:include="include :: select2-js"/>
<th:block th:include="include :: summernote-js"/>
<script th:inline="javascript">
  var prefix = ctx + "product/product";
  $("#form-product-edit").validate({
    focusCleanup: true
  });

  /*商品管理-新增-选择分类树*/
  function selectCategoryTree() {
    var categoryId = $.common.isEmpty($("#treeId").val()) ? "100" : $("#treeId").val();

    var url;
    if ($.common.isEmpty(categoryId)) {
      url = ctx + "product/category/selectCategoryTree/";
    } else {
      url = ctx + "product/category/selectCategoryTree/" + categoryId;
    }

    var options = {
      title: '选择分类',
      width: "380",
      url: url,
      callBack: doSubmit
    };
    $.modal.openOptions(options);
  }

  function doSubmit(index, layero) {
    var tree = layero.find("iframe")[0].contentWindow.$._tree;
    if ($.tree.notAllowParents(tree)) {
      var body = layer.getChildFrame('body', index);
      $("#treeId").val(body.find('#treeId').val());
      $("#treeName").val(body.find('#treeName').val());
      layer.close(index);
    }
  }

  $(document).ready(function () {
    $('.introduce').summernote({
      lang: 'zh-CN',
      height: 300,         // set editor height
      callbacks: {
        onImageUpload: function (files) {
          updateFile(files[0], '.introduce');
        }
      }
    });

    //初始化编辑器的值（内容会转义，所以加头部）
    $('.introduce').summernote('code',
      /*<![CDATA[*/
      [[${product.introduce}]]
      /*]]>*/
    );

    $('.specification').summernote({
      lang: 'zh-CN',
      height: 300,         // set editor height
      callbacks: {
        onImageUpload: function (files) {
          updateFile(files[0], '.specification');
        }
      }
    });

    //初始化编辑器的值（内容会转义，所以加头部）
    $('.specification').summernote('code',
      /*<![CDATA[*/
      [[${product.specification}]]
      /*]]>*/
    );
  });

  function updateFile(file, clazz) {
    var fileData = new FormData();
    fileData.append("file", file);

    $.ajax({
      url: "/common/upload",
      data: fileData,
      type: "post",
      contentType: false,
      processData: false,
      success: function (result) {
        if (result.code == web_status.SUCCESS) {
          var url = result.url;
          $(clazz).summernote('insertImage', url, 'image name');
        } else if (result.code == web_status.WARNING) {
          $.modal.alertWarning(result.msg)
        } else {
          $.modal.alertError(result.msg);
        }
      }
    });
  }

  function submitHandler() {
    if ($.validate.form()) {
      let formdata = new FormData();
      formdata.append("productId", $("input[name='productId']").val());
      formdata.append("productCode", $("input[name='productCode']").val());
      formdata.append("productName", $("input[name='productName']").val());
      formdata.append("orderNum", $("input[name='orderNum']").val());
      formdata.append("retailPrice", $("input[name='retailPrice']").val());
      formdata.append("currentPrice", $("input[name='currentPrice']").val());
      formdata.append("categoryId", $("#treeId").val());
      formdata.append("brandId", $.form.selectSelects("brand"));
      formdata.append("introduce", $('.introduce').summernote('code'));
      formdata.append("specification", $('.specification').summernote('code'));
      formdata.append("isPutaway", $("input[name='isPutaway']:checked").val());
      formdata.append("isNew", $("input[name='isNew']:checked").val());
      formdata.append("isRecommend", $("input[name='isRecommend']:checked").val());
      formdata.append("typeId", $("input[name='typeId']").val());
      formdata.append("remark", $("#remark").val());

      $.ajax({
        url: prefix + "/edit",
        data: formdata,
        type: "post",
        processData: false,
        contentType: false,
        success: function(result) {
          $.operate.saveSuccess(result);
        }
      });
      // $.operate.save(prefix + "/edit", $('#form-product-edit').serialize());/**/
    }
  }
</script>
</body>
</html>